package ge.telasi.tasks.ui.task;

import ge.telasi.tasks.model.Task;
import ge.telasi.tasks.model.TaskRelation;
import ge.telasi.tasks.ui.Platform;
import ge.telasi.tasks.ui.UIUtils;
import ge.telasi.tasks.ui.comp.TableModel;
import ge.telasi.tasks.ui.log.LoggerUtils;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * @author dimitri
 */
public class TaskRelationEditPanel extends JPanel {
    private TaskRelationTable table;
    private JButton btnAddRelation;
    private JButton btnRemoveRelation;
    private TaskSelectorDialog dialog;
    private JToolBar toolbar;
    private Task task;

    public TaskRelationEditPanel() {
        initComponents();
        table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                enableComponents();
            }
        });
        enableComponents();
    }

    private void initComponents() {
        setLayout(new BorderLayout());
        // add table
        table = new TaskRelationTable();
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        add(new JScrollPane(table), BorderLayout.CENTER);

        // toolbar
        btnAddRelation = new JButton(UIUtils.getIcon16x16("add.png"));
        btnRemoveRelation = new JButton(UIUtils.getIcon16x16("delete.png"));
        btnAddRelation.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onAdd();
            }
        });
        btnRemoveRelation.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onRemove();
            }
        });
        toolbar = new JToolBar();
        toolbar.add(btnAddRelation);
        toolbar.add(btnRemoveRelation);
        toolbar.setFloatable(false);
        setToolbarVisible(true);
    }

    private void enableComponents() {
        boolean selected = table.getSelection() != null;
        btnAddRelation.setEnabled(true);
        btnRemoveRelation.setEnabled(selected);
    }

    public List<TaskRelation> getRelations(boolean asParent) {
        return table.getRelations(asParent);
    }

    public void setRelations(List<TaskRelation> relations, boolean asParent) {
        table.setRelations(relations, asParent);
    }

    public Task getTask() {
        return task;
    }

    public void setTask(Task task) {
        this.task = task;
    }

    private void onAdd() {
        if (dialog == null) {
            dialog = new TaskSelectorDialog(TaskRelationEditPanel.this);
        }
        UIUtils.openDialog(dialog);
        if (dialog.isApproved()) {
            List<Task> tasks = dialog.getSelections();
            try {
                if (task != null) {
                    List<TaskRelation> relations = new ArrayList<TaskRelation>();
                    for (Task prnt : tasks) {
                        TaskRelation relation = new TaskRelation();
                        relation.setParent(prnt);
                        relations.add(relation);
                    }
                    Platform pl = Platform.getDefault();
                    if (pl.validateConnection()) {
                        task = pl.getFacade().addTaskParentRelation(pl.getCredentials(), task, relations);
                        fireTaskChange();
                    }
                }
                TableModel<Task> model = (TableModel<Task>) table.getModel();
                for (Task t : tasks) {
                    List<Task> data = model.getData();
                    if(data == null || !data.contains(t)) {
                        model.addItem(t);
                    }
                }
            } catch (Exception ex) {
                LoggerUtils.manageException(this, ex);
            }
        }
    }

    private void onRemove() {
        Platform pl = Platform.getDefault();
        List<Task> tasks = table.getSelections();
        String msg = pl.getLocaleManager().getMessage("General.deleteByCountMsg", tasks.size());
        if (UIUtils.askConfirmation(TaskRelationEditPanel.this, msg)) {
            try {
                if (task != null) {
                    List<TaskRelation> relations = new ArrayList<TaskRelation>();
                    for (Task prnt : tasks) {
                        TaskRelation relation = new TaskRelation();
                        relation.setParent(prnt);
                        relations.add(relation);
                    }
                    if (pl.validateConnection()) {
                        task = pl.getFacade().removeTaskParentRelation(pl.getCredentials(), task, relations);
                        fireTaskChange();
                    }
                }
                TableModel<Task> model = (TableModel<Task>) table.getModel();
                for (Task t : tasks) {
                    model.removeItem(t);
                }
            } catch (Exception ex) {
                LoggerUtils.manageException(this, ex);
            }
        }
    }

    public void setToolbarVisible(boolean visible) {
        remove(toolbar);
        if (visible) {
            add(toolbar, BorderLayout.SOUTH);
        }
        doLayout();
    }

    protected void fireTaskChange() {
        firePropertyChange("task", "dummy old value", task);
    }

}
